package com.nuanshui.heatedloan.credit.rabbitmq;

import com.nuanshui.framework.lang.DateUtil;
import com.nuanshui.framework.springContext.SpringPropertiesUtil;
import com.nuanshui.framework.utils.LogUtil;
import com.nuanshui.heatedloan.constant.RabbitMqQueue;
import com.nuanshui.heatedloan.credit.constant.OrderType;
import com.nuanshui.heatedloan.credit.facade.PayOrderInfoService;
import com.nuanshui.heatedloan.credit.facade.job.PayOrderJobService;
import com.nuanshui.heatedloan.credit.msg.CreditPushConstant;
import com.nuanshui.heatedloan.credit.msg.CreditPushMsgService;
import com.nuanshui.heatedloan.credit.request.OrderJobParams;
import com.nuanshui.heatedloan.credit.vo.PayOrderInfoVO;
import com.nuanshui.heatedloan.middleware.rabbitmq.RabbitmqMessageHandler;
import com.nuanshui.heatedloan.middleware.rabbitmq.annotation.MqMessageHandler;
import com.nuanshui.heatedloan.msgcenter.integration.CreditPushMsgBody;
import com.nuanshui.heatedloan.task.facade.constant.TaskEnum;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * Created by wangxw on 2017/7/25.
 * 逾期提醒消息队列
 * 这里面最多生成9次，
 * 还有一次是在贷款时间到的时候生成的
 * 每天早上9点开始执行
 */

@MqMessageHandler(RabbitMqQueue.RABBIT_MQ_LATE_REMIND_JOB)
public class DealLateRemindHandler implements RabbitmqMessageHandler {

    private static Logger logger = LoggerFactory.getLogger("DealLateRemindHandler");

    @Autowired
    private PayOrderInfoService payOrderInfoService;

    @Autowired
    private PayOrderJobService payOrderJobService;

     //消息和推送
    @Autowired
    private CreditPushMsgService creditPushMsgService;

    public void onMessage(Message message, Channel channel ) throws Exception{
        try{
            //生成下一个逾期的提醒的
            int hour=Integer.parseInt(SpringPropertiesUtil.getPropertiestValue("credit.reminder.hour"));
            LogUtil.info(logger, "------开始提醒的时间为"+hour+"------");
            LogUtil.info(logger, "-------------开始处理订单逾期提醒队列-----------");
            byte[] body = message.getBody();
            String orderNum = new String(body);
            if (orderNum.endsWith("\"")) {
                orderNum = orderNum.substring(1, orderNum.length() - 1);
            }
            LogUtil.info(logger, String.format("---------------------orderNum : %s-----------", orderNum));
            PayOrderInfoVO payOrderInfoVO=payOrderInfoService.loadVOByOrderNo(orderNum);
            Date shouldReturnTime=payOrderInfoVO.getShouldReturnTime();
            long day= DateUtil.getIntervalDay(new Date(),shouldReturnTime);
            if(day<10&&payOrderInfoVO.getOrderStatus()== OrderType.OVERDUEPAYMENT){
                Date lateDate=DateUtil.getDateHourMinuteSecond(new Date(),hour,0,0);
                Date lateNextCostDateDateUtil=DateUtil.addDays(lateDate,1);
                OrderJobParams lateRemindJobParams=new OrderJobParams();
                lateRemindJobParams.setOrderNo(orderNum);
                lateRemindJobParams.setOrderExpireTime(lateNextCostDateDateUtil);
                lateRemindJobParams.setTaskType(TaskEnum.LATE_REMIND_JOB);
                payOrderJobService.createExpireRefundJob(lateRemindJobParams);
            }
            LogUtil.info(logger, "-------------处理订单逾期提醒队列完成------------");
            LogUtil.info(logger, "-------------开始发送订单逾期提醒推送和短信------------");
            this.sendPushAndMsg(payOrderInfoVO,day);
            LogUtil.info(logger, "-------------完成发送订单逾期提醒推送和短信------------");
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }
    }
    //发送消息和队列
    private void sendPushAndMsg(PayOrderInfoVO payOrderInfoVO,long day){
        CreditPushMsgBody creditPushMsgBody =new CreditPushMsgBody();
        creditPushMsgBody.addDstUserId(payOrderInfoVO.getUserId());
        creditPushMsgBody.setType(CreditPushConstant.OVERDU_REMIND_TYPE);
        creditPushMsgBody.setOverduDay((int)day);
        creditPushMsgBody.setAmount(payOrderInfoVO.getLoanAmount());
        creditPushMsgBody.setRemainDay(0);
        creditPushMsgService.dealPush(creditPushMsgBody);
    }

}
